
package xj.graph2d.app.resources;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import xj.graph2d.DrawAttr;
import xj.graph2d.DrawShape;
import xj.graph2d.EllipseShape;
import xj.graph2d.LineShape;
import xj.graph2d.OpenShape;
import xj.graph2d.RectangleShape;
import xj.graph2d.shapes.CircleShape;
import xj.graph2d.shapes.CubicCurveShape;
import xj.graph2d.shapes.OrthogonalLineShape;
import xj.graph2d.shapes.PolyCurveShape;
import xj.graph2d.shapes.PolyLineShape;
import xj.graph2d.shapes.PolygonShape;
import xj.graph2d.shapes.QuadCurveShape;
import xj.graph2d.shapes.RoundRectangleShape;
import xj.graph2d.shapes.ShapeConstants;
import xj.graph2d.shapes.ShapeIcon;
import xj.util.gui.ResizableImageIcon;

public class Icons { 

  public static void loadIcons(Map<String, Icon> iconmap) { 
    if (iconmap != null) { 
      LineShape line = new LineShape(0, 20, 20, 0);
      setAttr(line);
      ShapeIcon lineIcon = new ShapeIcon(line, iconWidth, iconHeight);
      iconmap.put("lineIcon", lineIcon);

      QuadCurveShape arc = new QuadCurveShape(0, 20, 20, 00, 0, 0);
      setAttr(arc);
      ShapeIcon arcIcon = new ShapeIcon(arc, iconWidth, iconHeight);
      iconmap.put("arcIcon", arcIcon);
      
      CubicCurveShape curve = new CubicCurveShape(0, 20, 20, 0, 0, 5, 20, 15);
      setAttr(curve);
      ShapeIcon curveIcon = new ShapeIcon(curve, iconWidth, iconHeight);
      iconmap.put("curveIcon", curveIcon);

      PolyLineShape pline = new PolyLineShape(0, 5);
      pline.addPoint(5, 20);
      pline.addPoint(12, 0);
      pline.addPoint(20, 10);
      setAttr(pline);
      ShapeIcon plineIcon = new ShapeIcon(pline, iconWidth, iconHeight);
      iconmap.put("polylineIcon", plineIcon);

      PolyCurveShape pcurve = new PolyCurveShape(0, 20);
      pcurve.addPoint(5, 5);
      pcurve.addPoint(12, 15);
      pcurve.addPoint(20, 10);
      pcurve.setCtr1(5, 5);
      pcurve.setCtr2(15, 10);
      pcurve.calculateBounds();
      setAttr(pcurve);
      ShapeIcon pcurveIcon = new ShapeIcon(pcurve, iconWidth, iconHeight);
      iconmap.put("polycurveIcon", pcurveIcon);

      OrthogonalLineShape oline = new OrthogonalLineShape(true, 0, 0);
      oline.addPoint(5, 20);
      oline.addPoint(12, 12);
      oline.addPoint(20, 8);
      oline.calculateBounds();
      setAttr(oline);
      ShapeIcon olineIcon = new ShapeIcon(oline, iconWidth, iconHeight);
      iconmap.put("orthlineIcon", olineIcon);

      RectangleShape rect = new RectangleShape(0, 0, 20, 16);
      setAttr(rect);
      ShapeIcon rectIcon = new ShapeIcon(rect, iconWidth, iconHeight);
      iconmap.put("rectIcon", rectIcon);

      RoundRectangleShape rrect = new RoundRectangleShape(0, 0, 20, 16, 3, 3);
      setAttr(rrect);
      ShapeIcon rrectIcon = new ShapeIcon(rrect, iconWidth, iconHeight);
      iconmap.put("roundRectIcon", rrectIcon);

      EllipseShape oval = new EllipseShape(0, 0, 20, 16);
      setAttr(oval);
      ShapeIcon ovalIcon = new ShapeIcon(oval, iconWidth, iconHeight);
      iconmap.put("ovalIcon", ovalIcon);

      CircleShape circ = new CircleShape(0, 0, 10);
      setAttr(circ);
      ShapeIcon circIcon = new ShapeIcon(circ, iconWidth, iconHeight);
      iconmap.put("circleIcon", circIcon);

      PolygonShape polygon = new PolygonShape(0, 5);
      polygon.addPoint(5, 0);
      polygon.addPoint(15, 5);
      polygon.addPoint(20, 10);
      polygon.addPoint(10, 18);
      polygon.addPoint(0, 15);
      setAttr(polygon);
      ShapeIcon polygonIcon = new ShapeIcon(polygon, iconWidth, iconHeight);
      iconmap.put("polygonIcon", polygonIcon);

      loadRectangularShapeIcons(iconmap);

      loadImageIcons(iconmap);
    }
  }

  public static void loadRectangularShapeIcons(Map<String, Icon> iconmap) {
    if (iconmap != null) { 
      for (int i = 0; i < ShapeConstants.shapeKeys.length; i++) {
	String name = ShapeConstants.shapeKeys[i] + "Icon";
	RectangularShapeIcon icon = new RectangularShapeIcon(i, iconWidth, iconHeight);
	DrawShape shape = icon.getShape();
	setAttr(shape);
	iconmap.put(name, icon);
      }
    }
  }

  static DrawShape setAttr(DrawShape shape) {
    if (shape != null) { 
      DrawAttr attr = shape.getDrawAttr();
      if (attr == null) {
	attr = new DrawAttr();
	shape.setDrawAttr(attr);
      }
      attr.setPenColor(color);

      if (!(shape instanceof OpenShape)) { 
	shape.setFilled(true);
	attr.setFillColor(bgColor1);
	Color gradientColor1 = bgColor1;
	Color gradientColor2 = bgColor2;
	  //DrawingUtilities.darkerColor(bgColor1, 0.8f);	  
	attr.setGradientColor1(gradientColor1);
	attr.setGradientColor2(gradientColor2);
	attr.setGradientMode(DrawAttr.LINEAR_GRADIENT_LEFT_TO_RIGHT);
      }
    }
    return shape;
  }

  public static void loadImageIcons(Map<String, Icon> iconmap) {
    if (iconmap != null) { 
      BufferedImage img = new BufferedImage(12, 12, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g = (Graphics2D) img.getGraphics();
      g.setColor(Color.green);
      g.fillRect(0, 0, 12, 12);
      g.setColor(Color.white);
      g.fillRect(2,2,4,4);
      g.setColor(Color.yellow);
      g.fillOval(6,6,5,5);
      Icon icon = new ResizableImageIcon(img);
      
      iconmap.put("Stencil", icon);
    }
  }

  static int iconWidth = 16, iconHeight = 16;
  static Color color = DrawAttr.defaultPenColor;
  //new Color(25, 25, 112);
  //static Color color = Color.blue;
  static Color bgColor1 = new Color(234, 234, 174, 64);
  static Color bgColor2 = new Color(202, 255, 112, 64);
  //new Color(255, 255, 240, 128);

}
